package com.lzy.webapi.controller;

import com.github.pagehelper.PageInfo;
import com.lzy.framework.core.ensure.Ensure;
import com.lzy.framework.core.orm.db.Pager;
import com.lzy.framework.core.output.Result;
import com.lzy.framework.core.utils.DozerUtils;
import com.lzy.product.apis.ProductInfoServiceApi;
import com.lzy.product.dtos.ProductInfoDto;
import com.lzy.webapi.component.UserToken;
import com.lzy.webapi.facade.ProductFacade;
import com.lzy.webapi.request.ProductRequest;
import com.lzy.webapi.response.ProductInfoResponse;
import com.lzy.webapi.response.ProductResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 产品管理接口
 * Created by WIN7 on 2017/12/1.
 */
@Controller
@RequestMapping("api/product")
public class ProductInfoController extends BaseController{
    private static final Logger LOGGER = LoggerFactory .getLogger(ProductInfoController.class);

    @Autowired
    ProductInfoServiceApi productInfoServiceApi;

    @Autowired
    ProductFacade productFacade;

    /**
     * 业务员的产品列表
     * @param
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public @ResponseBody Result getProductList(@RequestHeader("token") String token,ProductRequest request) {
        UserToken ut = isLogin(token);
        Ensure.that(ut).isNotNull("01000006");
        Pager pager = new Pager();
        pager.setPageNum(request.getPageNum());
        pager.setPageSize(request.getPageSize());
        PageInfo<ProductInfoDto> pageInfo = productFacade.getProductList(pager,ut.getUserId());
        List<ProductResponse> productResponseList = checkProductResponse(pageInfo);
        return new Result(productResponseList);
    }

    /**
     * 产品详情
     * @param
     * @return
     */
    @RequestMapping(value = "/detail/{id}", method = RequestMethod.GET)
    public @ResponseBody Result getProductDetail(@RequestHeader("token") String token, @PathVariable("id") Long id) {
        UserToken ut = isLogin(token);
        Ensure.that(ut).isNotNull("01000006");
        ProductInfoResponse productInfoResponse = productFacade.getProductDetail(id);
        if(productInfoResponse != null){
            return new Result(productInfoResponse);
        }
        return new Result();
    }

    /**
     * 所有产品列表
     * @param
     * @return
     */
    @RequestMapping(value = "/allProductList", method = RequestMethod.GET)
    public @ResponseBody Result getAllProductList(ProductRequest request) {
        Pager pager = new Pager();
        pager.setPageNum(request.getPageNum());
        pager.setPageSize(request.getPageSize());
        PageInfo<ProductInfoDto> pageInfo = productInfoServiceApi.selectList(pager, DozerUtils.map(request, ProductInfoDto.class));
        List<ProductResponse> productResponseList =checkProductResponse(pageInfo);
        return new Result(productResponseList);
    }

    public List<ProductResponse> checkProductResponse(PageInfo<ProductInfoDto> pageInfo){
        List<ProductResponse> productResponseList = new ArrayList<>();
        if (pageInfo != null && pageInfo.getList() != null) {
            List<ProductInfoDto> proList = pageInfo.getList();
            for(ProductInfoDto pro : proList){
                ProductResponse proResponse = new ProductResponse();
                if(pro != null){
                    proResponse.setProductId(String.valueOf(pro.getId()));
                    proResponse.setProductName(pro.getName());
                    proResponse.setProductImageUrl(pro.getCover());
                    productResponseList.add(proResponse);
                }
            }
        }
        return productResponseList;
    }
}